<?php
class UsersController extends AppController {
	public $name='Users';
	public $helpers = array('Html', 'Form');
	var $components = array('Session');
	public function index(){
		$this->set('users', $this->User->find('all'));
	} 
	
	function beforeFilter(){
		$this->Auth->allow('register');
		parent::beforeFilter();
	}
	
	public function login(){
		$this->layout = 'one_column';
		if ($this->request->is('post')) {
			if ($this->Auth->login()) {
				echo $this->Session->setFlash(__('Logged In'));
		        return $this->redirect($this->Auth->redirect());
		    } else {
		        $this->Session->setFlash(__('Username or password is incorrect'));
		    }
		}
		
	}
	
	public function logout(){
		$this->redirect($this->Auth->logout());
	}
	
	public function register(){
		$this->layout = 'one_column';
		if ($this->request->is('post')) {
			if ($this->User->save($this->request->data)) {
				$id = $this->User->user_id;
				$this->request->data['User'] = array_merge($this->request->data['User'], array('user_id' => $id));
				$this->Auth->login($this->request->data['User']);
				$this->redirect('/home/index');
				$this->Session->setFlash(__('Your account has been created.'));
			} else {
				$this->Session->setFlash(__('Unable to add your account.'));
			}
		}
	}
}